package com.mall.manager.powerpart.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.mall.manager.powerpart.service.ManagerPowerPartService;

/**
 * 
 * @author LISIQIANG
 * 处理角色权限增、删、改、查控制层
 */

@Controller
public class ManagerPowerPartController {
	@Resource ManagerPowerPartService powerPartService;
     
    
    /**
     * 修改角色权限
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    
    
    @RequestMapping(value = "/manager_power_part_edit")    
    public void manager_power_part_edit(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {   
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	powerPartService.manager_power_part_edit(paraMap);
    	        
    }
       
    
    /**
     * 由角色id找出对应的权限
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/manager_power_part_assign") 
    public String manager_power_part_assign(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)throws ServletException, IOException{
    	 	
    	
    powerPartService.change_power_part_checked(request);
    	
    List<Map<String, Object>> partList= powerPartService.manager_power_part_assign(request,response,modelMap);
	
	modelMap.addAttribute("manager_part_list", partList);
	
    	    	
 	 List<Map<String, Object>> powerList = powerPartService.manager_power_listback(request);
 	modelMap.addAttribute("powerList", powerList);
 	
 	return "partpower/manager_partpower_list";
 	
    }
    
    /**
     *角色权限初始化查询处理
     * @param request
     * @return
     */
    @RequestMapping(value = "/manager_power_part_list")    
    public String manager_power_part_list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)throws ServletException, IOException {  
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");	
    	List<Map<String, Object>> manager_part_list= powerPartService.manager_power_part_list(request,response);
	    int partId =MapUtils.getIntValue(paraMap, "partId");
    	List<Map<String, Object>>  power1=powerPartService.getPowerbypartId(1);
		List<Map<String, Object>>  power2=powerPartService.getPowerbypartId(2);
		List<Map<String, Object>>  power3=powerPartService.getPowerbypartId(3);
		modelMap.addAttribute("partId", partId);
		modelMap.addAttribute("manager_part_list", manager_part_list);
		modelMap.addAttribute("power1", power1);
    	modelMap.addAttribute("power2", power2);
    	modelMap.addAttribute("power3", power3);
        return "partpower/manager_partpower_list";
    }
    
    /**
     *角色权限勾选处理
     * @param request
     * @return
     */
    @ResponseBody 
    @RequestMapping(value = "/manager_part_power_list")    
    public String manager_part_power_list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)throws ServletException, IOException {  
    	Map<String, Object> powerList = new HashMap<String, Object>();
    	List<Map<String, Object>>  powerList1=powerPartService.getPowerbypartId(request,1);
		List<Map<String, Object>>  powerList2=powerPartService.getPowerbypartId(request,2);
		List<Map<String, Object>>  powerList3=powerPartService.getPowerbypartId(request,3);
		powerList.put("level1",powerList1);
		powerList.put("level2",powerList2);
		powerList.put("level3",powerList3);
		JSONObject jsonObject = JSONObject.fromObject(powerList);
        return jsonObject.toString();
    }
    
    /**
     * 更新角色权限
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/manager_powerpart_update")    
    @ResponseBody
    public String manager_power_update(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	int update=powerPartService.manager_power_update(paraMap);
    	if(update==1){
    		return "success";
    	}else{
    		return "failure";
    	}
    }
}
